package com.cdyx.outsideprescription.controller;


import com.cdyx.outsideprescription.dto.queryEpcDto;
import com.cdyx.outsideprescription.service.ITbEpcinfoService;
import com.cdyx.outsideprescription.util.AppResponse;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiang dongqiong
 * @since 2021-12-27
 */
@Slf4j
@RestController
@RequestMapping("/outsideprescription")
public class TbEpcinfoController {
    @Qualifier("tbEpcinfoServiceImpl")
    @Autowired
    private ITbEpcinfoService epcinfoService;
    @ApiOperation("双通道电子处方保存并上传")
    @PostMapping("/createEpc")
    public AppResponse createEpc(@RequestBody Map<String,Object> data){
       try {
         long ID= epcinfoService.createEpc(data);
           epcinfoService.pushEpcInfo(data,ID);
           log.info("own_id===============>"+ID);
           if (ID==0){
               return AppResponse.fail();
           }
           return AppResponse.ok(ID);
       }catch (Exception e) {
           log.error(e.getMessage());
           return AppResponse.fail();
       }
    }

    @ApiOperation("双通道电子处方上传")
    @PostMapping("/uploadEpc")
    public AppResponse UploadEpc(@RequestBody Map<String,Object> data){
        try {
         Map<String, String> map = epcinfoService.pushEpcInfoReal(data);
            if (map.get("status").toString().equals("-1")){
                String message = map.get("message").toString();
                return AppResponse.fail(message);
            }else{
                return AppResponse.ok(map);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AppResponse.fail();
        }
    }

    @ApiOperation("双通道电子处方查询")
    @PostMapping("/queryEpcInfo")
    public AppResponse queryEpcInfo(@RequestBody Map<String,Object> data){
        List<Map<String, Object>> map=epcinfoService.queryEpcInfo(data);
        if (map!=null){
            return AppResponse.ok(map);
        }else {
            return AppResponse.fail();
        }
    }
    @ApiOperation("双通道电子处方撤销")
    @PostMapping("/undoEpcInfo")
    public AppResponse undoEpcInfo(@RequestBody Map<String,Object> data){
       try {
           int i = epcinfoService.undoEpc(data);
           if (i==-1){
               return AppResponse.fail();
           }else {
               return AppResponse.ok();
           }
       }catch (Exception e){
           return AppResponse.fail();
       }
    }
    @ApiOperation("保持成功后查询接口")
    @PostMapping("/queryEpc")
    public AppResponse queryEpc(@RequestBody Map<String,Object> data){
      List<queryEpcDto> map = epcinfoService.queryEpc(data);
     return AppResponse.ok(map);
 }


}
